package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.BaseRoleService;
import com.hhs.storage.service.ContractService;
import com.hhs.storage.service.FlowService;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: HuangHaiShui
 * @description:
 * @date: 2022/12/12 14:13
 * @version: 1.0
 */
@Service
@Transactional
public class WorkbenchService {
    @Resource
    FlowService flowService;

    @Resource
    BaseRoleService baseRoleService;

    @Resource
    ContractService contractService;

    //预警信息  
    public Map<String, Object> earlyWarning(String userId) {
        if(StringUtils.isEmpty(userId)){
            return ResponseInfo.success();
        }
        List<EarlyWarn> resList = new ArrayList<>();
        //获取该用户全部角色 ,并拼接成字符串
        List<BaseRole> roleList = baseRoleService.getByUserId(userId);
        String flagStr=",,";
        for (BaseRole baseRole : roleList) {
            flagStr+=baseRole.getFlag()+",";
        }
        //业务员角色
        if(flagStr.indexOf(SysUtil.ROLE_FLAG_YWY)>0){
            resList=YWY(userId);
        }
        //经理   查询是否有待审核的合同
        if(flagStr.indexOf(","+SysUtil.ROLE_FLAG_JL)>0){
            resList.addAll(JL(userId));
        }
        //总经理 查询是否有待审核的合同
        if(flagStr.indexOf(","+SysUtil.ROLE_FLAG_ZJL)>0){
            resList.addAll(ZJL(userId));
        }

        return ResponseInfo.success(resList);
    }

    /**
     * @Author: HuangHaiShui 
     * @Description: 业务员的所有预警功能
     * @Date: 15:24 2022/12/21
     * @param userId: 
     * @return: List<EarlyWarn>
     **/
    private  List<EarlyWarn> YWY(String userId){
        List<EarlyWarn> resList = new ArrayList<>();
        InputParam inputParam = new InputParam();
        inputParam.setCurUserId(userId);
        //查询是否有被驳回的合同
        List<Contract> checkReturn = contractService.getCheckReturn(userId);
        if(checkReturn.size()>0){
            EarlyWarn ew = new EarlyWarn(
                    EarlyWarn.TYPE_CONTRACT_YWYRETURN,
                    checkReturn.size(),
                    "/index/myTypeIn"
            );
            resList.add(ew);
        }
        //查询是否有快到期的合同 一个月以内的提醒
        List<Contract> expireList = contractService.getExpireCaution(inputParam);
        if(expireList.size()>0){
            EarlyWarn ew = new EarlyWarn(
                    EarlyWarn.TYPE_CONTRACT_EXP,
                    expireList.size(),
                    "/index/exp"
            );
            resList.add(ew);
        }
        //查询是否有临时合同，提醒改正式 合同表状态为5，并且未提交转正式
        List<Contract> tempList = contractService.getTempContract(inputParam);
        if(tempList.size()>0){
            EarlyWarn ew = new EarlyWarn(
                    EarlyWarn.TYPE_CONTRACT_TEMP,
                    tempList.size(),
                    "/index/myTypeIn"
            );
            resList.add(ew);
        }
        return resList;
    }

    //经理预警
    private  List<EarlyWarn> JL(String userId){
        List<EarlyWarn> resList = new ArrayList<>();
        InputParam inputParam = new InputParam();
        inputParam.setCurUserId(userId);
        inputParam.setStatus(Contract.STATUS_CHECK);
        inputParam.setIsFinished(false);
        //查询是否有待初审的合同
        List<Flow> flowList = flowService.getByParams(inputParam);
        if(flowList.size()>0){
            EarlyWarn ew = new EarlyWarn(
                    EarlyWarn.TYPE_CHECK,
                    flowList.size(),
                    "/index/check"
            );
            resList.add(ew);
        }
        return resList;
    }
    //总经理预警
    private  List<EarlyWarn> ZJL(String userId){
        List<EarlyWarn> resList = new ArrayList<>();
        InputParam inputParam = new InputParam();
        inputParam.setCurUserId(userId);
        inputParam.setStatus(Contract.STATUS_CHECK_FINAL);
        inputParam.setIsFinished(false);
        //查询是否有待终审的合同
        List<Flow> flowList = flowService.getByParams(inputParam);
        if(flowList.size()>0){
            EarlyWarn ew = new EarlyWarn(
                    EarlyWarn.TYPE_CHECK_FINAL,
                    flowList.size(),
                    "/index/checkFinal"
            );
            resList.add(ew);
        }
        return resList;
    }
}
